package reflectDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectTest {
    public static void main(String[] args) {
        reflectPrivateMethod();
//        reflectPrivateField();
//        reflectPrivateConstructor();
//        reflectNewInstance();
    }

    // 反射私有方法
    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Method function = c1.getDeclaredMethod("function", String.class);
            Method function2 = c1.getDeclaredMethod("eat");
            function.setAccessible(true);
            function2.setAccessible(true);
            Student student = (Student) c1.newInstance();
            function.invoke(student, "我是参数！");
            function2.invoke(student);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有属性
    public static void reflectPrivateField() {
        // 获取类对象
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Field field = c1.getDeclaredField("name");
            // 确认机制
            field.setAccessible(true);
            // 实例化对象
            Student student = (Student)c1.newInstance();
            field.set(student, "zhangfei");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            // 获取类对象
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Constructor<?> declaredConstructor = c1.getDeclaredConstructor(String.class, int.class);
            // 确认机制
            declaredConstructor.setAccessible(true);
            // 实例化对象
            Student student = (Student) declaredConstructor.newInstance("zhangsan", 18);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //创建类对象
    public static void reflectNewInstance(){
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Student student = (Student) c1.newInstance(); // newInstance 创建类的实例
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }


    // 获取类对象
    public static void main1(String[] args) throws ClassNotFoundException {
        // 方法一
        Class<?> c1 = Class.forName("reflectDemo.Student");
        // 方法二
        Class<?> c2 = Student.class;
        // 方法三
        Student student = new Student();
        Class<?> c3 = student.getClass();

        System.out.println(c1.equals(c2));
        System.out.println(c2.equals(c3));
        System.out.println(c1.equals(c3));
    }
}
